<?php
namespace jy_robshop;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class game extends \plugin{
	//time,参与时机'1'=>'抢中N元的倍数'
	public function up_addgame($post=array(),$mid){
		$mid=$mid?:$_SESSION['uid'];
		$setting=$this->plugin_setting('game');
		$params=array('action'=>'jy_robshop/game/','plugin_name'=>'jy_robshop','uniacid'=>$_SESSION['uniacid']);
		$game=pdo_get('jy_weishop_game',$params);
		$game['pluginset']=json_decode($game['pluginset'],true);
		if($game && $game['price']>0){
			$times=intval($post['price']/$game['price']);
			$indata=array(
							'mid'=>$mid,
							'game_id'=>$game['id'],
							'action'=>$game['action'],
							'plugin_name'=>$game['plugin_name'],
							'type'=>1,
							'remark'=>'参与抢购(赠送的抽奖机会)',
							'times'=>$times,
			);
			$this->add_game($indata);
		}
		if($game && $game['pluginset']['zprice']>0){
			$times=intval($post['price']/$game['pluginset']['zprice']);
			$indata=array(
							'mid'=>$mid,
							'game_id'=>$game['id'],
							'action'=>$game['action'],
							'plugin_name'=>$game['plugin_name'],
							'type'=>2,
							'remark'=>'参与抢购(赠送的特殊抽奖机会)',
							'times'=>$times,
			);
			$this->add_game($indata);
		}
		return true;
	}
	public function add_game($params,$url=''){
		if($params['times']<=0){
			return false;
		}
		for($i=1;$i<=$params['times'];$i++){
			$new_game_wait=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$params['mid'],
				'game_id'=>$params['game_id'],
				'plugin_name'=>$params['plugin_name'],
				'action'=>$params['action'],
				'type'=>$params['type'],
				'status'=>0,
				'createtime'=>TIMESTAMP,
				'remark'=>$params['remark'],
			);
			pdo_insert('jy_weishop_game_wait',$new_game_wait);
		}
		$setting=$this->plugin_setting('game');
		$url='jy_robshop/game//id='.$params['game_id'];
		$news=array(
			'title'=>'恭喜，您获得活动参与资格',
			'description'=>"快来参加吧，礼品多多哦！",
			'url'=>custom_url($url,true),
			'picurl'=>$setting['share_image'],
		);
		model('core/account')->send_news($news,$params['mid']);
		return true;
	}
	//检查游戏剩余次数
	public function check_num($gameid=0){
		$num=pdo_count('jy_weishop_game_wait',array('status'=>0,'game_id'=>$gameid,'mid'=>$_SESSION['uid']));
		return $num;
	}
	//检查游戏权限
	public function check_game($id=0,$gameid=0){
		if($id){
			$wait=$this->pdo_get('game_wait',array('status'=>0,'id'=>$id,'mid'=>$_SESSION['uid']));
		}else{
			$wait=$this->pdo_get('game_wait',array('status'=>0,'game_id'=>$gameid,'mid'=>$_SESSION['uid']));
		}
		if(!$wait['game_id']){
				exi('你暂无参与该游戏的名额！','error');
		}
		if($wait['game_id']){
			$wait['game_id']=pdo_getcolumn('jy_weishop_game',array('id'=>$wait['game_id']),'game_id');
			return true;
		}elseif($wait['plugin_name'] && $wait['plugin_name']!='core'){
			$gameinfo=$this->plugin($wait['plugin_name'])->get_active($id);
			return $gameinfo;
		}
		return false;
	}
	//检查游戏已参与次数
	public function canyu_num($gameid=0){
		$num=$this->count('game_getlog',array('game_id'=>$gameid,'mid'=>$_SESSION['uid']));
		return $num;
	}
	//获取中奖LOG
	public function get_log($game_id=0,$page=1){
		$params=array('game_id'=>$game_id,'uniacid'=>$_SESSION['uniacid']);
		$pindex = max(1, intval($page));
		$psize = 10;
		$total='';
		$orderby='id desc';
		$parameter=array(
				'params'=>$params,
				'limit'=>array($pindex,$psize),
				'orderby'=>$orderby,
		);
		$logs=$this->pdo_getlist('game_getlog',$parameter,$total);
		return $logs;
	}
	//确认
	public function check_gongcan($reward_info=array(),$reward_key=0){
		$redfeelog=pdo_getsum('jy_robshop_redfeelog',array('type'=>0,'uniacid'=>$_SESSION['uniacid']),'fee');
		$redfee_rate=intval($reward_info['redfee_rate']);
		if($redfeelog<=0 || $redfee_rate<=0){
			exi('该奖品的奖金池不足，不能参与抽奖！','error');
		}
		$redfee_ratefee=$redfee_rate*$redfeelog/100;
		$isfeelog=pdo_getsum('jy_robshop_redfeelog',array('reward_key'=>$reward_key,'type'=>array(1,2),'uniacid'=>$_SESSION['uniacid']),'fee');
		if($isfeelog>=$redfee_ratefee || $redfee_ratefee<=0){
			exi('该奖品的奖金池不足，不能参与抽奖！','error');
		}
		if($reward_info['type']==4 && false){
			$redfeelog=pdo_getsum('jy_robshop_redfeelog',array('type'=>0,'uniacid'=>$_SESSION['uniacid']),'num');
			$redfee_rate=intval($reward_info['redfee_rate']);
			if($redfeelog<=0 || $redfee_rate<=0){
				exi('该奖品的奖金池不足，不能参与抽奖！','error');
			}
			$redfee_ratefee=$redfee_rate*$redfeelog/100;
			$isfeelog=pdo_getsum('jy_robshop_redfeelog',array('reward_key'=>$reward_key,'type'=>2,'uniacid'=>$_SESSION['uniacid']),'num');
			if($isfeelog>=$redfee_ratefee || $redfee_ratefee<=0){
				exi('该奖品的奖金池不足，不能参与抽奖！','error');
			}
		}
		return true;
	}
	//抽奖
	public function get_choujiang($game_id=0){
		if ($game_id) {
			$game=pdo_get('jy_weishop_game',array('id'=>$game_id,'uniacid'=>$_SESSION['uniacid']));
			$game['reward']=json_decode($game['reward'],true);
			$game['pluginset']=json_decode($game['pluginset'],true);
		}
		if($game['type']!=9){
			$check_num=$this->check_num($game_id);
			if($check_num<=0){
				exi('您已经没有抽奖次数了！','error');
			}
			$wait=pdo_get('jy_weishop_game_wait',array('game_id'=>$game_id,'mid'=>$_SESSION['uid'],'status'=>0));
			$waitid=$wait['id'];
		}else{
			$myredfee=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']), 'redfee');
			if($myredfee<$game['delredfee']){
				exi('您当前红包余额不足！','error');
			}
		}
		$reward=$game['reward'];		
		$reward_info = $this->getreward($reward,$game['pluginset'],$wait['type']);
		if (empty($reward_info)) {
			$is_reward = 0;
			$reward_title = '很遗憾,没有中奖';
			$reward_info=array(
				'title'=>'很遗憾,没有中奖',
				'key'=>-1,
			);
		}else{
			$reward_title = $reward_info['title'];
			$is_reward = 1;	
		}		
		$log_data = array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'game_id'=>$game['id'],
			'reward'=>ijson_encode($reward_info),
			'rewardkey'=>$reward_info['key'],
			'status'=>0,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_game_getlog', $log_data);
		$logid=pdo_insertid();
		if($game['type']!=9){
			pdo_update('jy_weishop_game_wait',array('status'=>1),array('id'=>$waitid,'mid'=>$_SESSION['uid']));
		}else{
			pdo_update('jy_weishop_member',array('redfee -='=>$game['delredfee']),array('mid'=>$_SESSION['uid']));
		}
		if($reward_info['type']==4){
			$credit4_rate=$game['pluginset']['credit4_rate']?:1;
			if($reward_info['jifen']){
				$goods_price=$reward_info['jifen']/$credit4_rate;
			}
		}else{
			$goods_price=$reward_info['goods_price'];
		}
		$return = array(
			'status' =>1,
			'id' => $reward_info['key'],
			'info' => $reward_title,
			'reward_info' => $reward_info,
			'is_reward' => $is_reward,
			'logid' => $logid,
			'type' => $reward_info['type']==4?2:1,
			'num' => $reward_info['jifen']?:1,
			'goods_price'=>$goods_price
		);
		if($reward_info['type']!=10 && $is_reward){
			$this->add_redfeelog($return);
		}
		return $return;
	}
	//领取奖品
	public function get_reward($game_id=0,$reward_id=0,$log_id=0){
		$mid=$mid?:$_SESSION['uid'];
		if (!empty($game_id)) {
			$game=pdo_get('jy_weishop_game',array('id'=>$game_id,'uniacid'=>$_SESSION['uniacid']));
			$game['reward']=json_decode($game['reward'],true);
		}
		$reward=$game['reward'];
		$reward=$reward[$reward_id];
		$fee=$this->set_reward($reward,$mid,$game,$log_id);
		$reward['isgetnum'] += 1;
		if ($reward['type']==1) {
		}elseif ($reward['type']==7) {
			$reward['isgetfee'] += $fee;
		}else{
			$reward['isgetfee'] += $reward['jifen'];
		}
		$temreward = $game['reward'];
		$temreward[$reward_id]=$reward;
		$updatedata = array('reward' => ijson_encode($temreward));
		$res=pdo_update('jy_weishop_game',$updatedata,array('uniacid' => $_SESSION['uniacid'],'id' =>$game_id));
		if ($res !== false) {
			$info = array(
				'status' => 1,
				'fee' => $fee,
				'info' => '恭喜您已获得《' . $temreward[$reward_id]['title'].'》'
			);
			return $info;
		}else{
			$info = array('status' => 0, 'info' => '获取奖励失败');
			return $info;
		}
	}
	public function set_reward($award,$mid,$game=array(),$log_id=0,$isexi=false){
		$mid=$mid?:$_SESSION['uid'];
		if (empty($award) || empty($mid)) {
			return false;
		}
		//币奖励
		if($award['type']!=1 && $award['type']!=7){
			$awardstatus=2;
			$fee=$award['jifen']?:0;
		}elseif($award['type']==1){
			$awardstatus=1;
		}elseif($award['type']==7){
			$red_max=$award['rexmax']?:0;
			$red_min=$award['redmin']?:0;
			$fee=mt_rand($red_min*100,$red_max*100);
			$fee=$fee/100;
			$red=array(
				'mid'=>$mid,
				'money'=>$fee,
				'plugin'=>$game['plugin_name'],
				'remark'=>'参加活动，活动现金红包',
			);
			$awardstatus=2;
		}
		$game['pluginset']=json_decode($game['pluginset'],true);
		if($award['type']!=1 && $award['type']!=7 && $award['type']!=10){
			$credittype=$award['type']==2?1:$award['type'];
			if($fee>0){
				if($game['merchid']){
					model('jy_merch/member')->addcreditlog($game['merchid'],'',$fee,$credittype,1);
				}else{
					cfc('core')->member_credit($mid,'credit'.$credittype,$fee,1,'jy_robshop','参与大转盘活动奖励');
				}
			}
		}elseif($award['type']==10){
			$indata=array(
					'mid'=>$mid,
					'game_id'=>$game['id'],
					'action'=>$game['action'],
					'plugin_name'=>$game['plugin_name'],
					'type'=>1,
					'remark'=>'获奖再来一次',
					'times'=>1,
			);
			$this->add_game($indata);
		}
		pdo_update('jy_weishop_game_getlog',array('fee'=>$fee,'status'=>$awardstatus), array('id'=>$log_id));
		return $fee;
	}
	public function add_redfeelog($data=array()){
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'type'=>$data['type'],
			'num'=>$data['num'],
			'fee'=>$data['goods_price'],
			'gamelogid'=>$data['logid'],
			'reward_key'=>$data['id'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		$this->insert('redfeelog',$data);
		return true;
	}
	private function getreward($reward=array(),$gamepluginset=array(),$waittype=0){
		foreach($reward as $key => &$item){
			$item['key']=$key;
			if($waittype==2){
				//只留下再来一次的
				if($item['type']!=10){
					unset($reward[$key]);
				}
			}else{
				$allfee = pdo_getsum('jy_robshop_redfeelog',array('type'=>0,'reward_key'=>$key,'uniacid'=>$_SESSION['uniacid']),'fee');
				$isfee = pdo_getsum('jy_robshop_redfeelog',array('type'=>array(1,2),'reward_key'=>$key,'uniacid'=>$_SESSION['uniacid']),'fee');
				$item['lastfee']=$allfee-$isfee;
				if($item['type']==4){
					$credit4_rate=$gamepluginset['credit4_rate']?:1;
					if($item['jifen']){
						$item['goods_price']=$item['jifen']/$credit4_rate;
					}
				}
				if($item['lastfee']>=$item['goods_price']){
					$item['canzhong']=1;
				}else{
					unset($reward[$key]);
				}
			}
		}
		paixu($reward,'goods_price','desc');
		$reward=array_values($reward);
		return $reward[0];
	}
	private function getRand($proArr)
	{
		$result = '';
		$proSum = array_sum($proArr);
		foreach ($proArr as $key => $proCur) {
			$randNum = mt_rand(1, $proSum);
			if ($randNum <= $proCur) {
				$result = $key;
				break;
			}
			$proSum -= $proCur;
		}
		unset($proArr);
		return intval($result);
	}
}